<?php
namespace app\common\model\shop;

use think\Model;
use think\Db;
use traits\model\SoftDelete;

class Goods extends Model
{
	// 设置当前模型使用的完整数据库连接信息
	protected $connection = 'database.db_second';//连接配置
	use SoftDelete;
    // 表名
    protected $name = 'shop_goods';
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;
    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
	protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
	    'nickname',
	    'avatar',
    	'images_url',
        'is_show_text',
        'is_hot_text',
        'status_text',
        'delatetime_text',
        'create_time_text'
    ];

	public function getNicknameAttr($value, $data) {
		$value = Db::name("user")->where(['id'=>$data['user_id']])->value('nickname');
		return $value ? $value : '';
	}

	public function getAvatarAttr($value, $data){
		$value = Db::name("user")->where(['id'=>$data['user_id']])->value('avatar');
		return $value ? $value : '';
	}
    
    public function getIsShowList()
    {
        return ['1' => __('上架'), '2' => __('下架')];
    }

    public function getIsHotList()
    {
        return ['1' => __('推荐'), '2' => __('正常')];
    }

    public function getStatusList()
    {
        return ['1' => __('正常'), '2' => __('违规下架')];
    }

    public function getIsShowTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_show']) ? $data['is_show'] : '');
        $list = $this->getIsShowList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getIsHotTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_hot']) ? $data['is_hot'] : '');
        $list = $this->getIsHotList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getDelatetimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['delatetime']) ? $data['delatetime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getCreateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setDelatetimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setCreateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    public function user()
    {
        return $this->belongsTo('app\common\model\User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function category()
    {
        return $this->belongsTo('app\common\model\Category', 'cate_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

	public function getImagesUrlAttr($value, $data){
		$value = $value ?: $data["images"];
		return getImagesUrl($value);

	}
}
